package com.ship.common.business.mapping;

import com.ship.common.core.utils.FieldUtil;
import javassist.NotFoundException;
import javassist.bytecode.ConstPool;
import javassist.bytecode.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 31463
 */
public class MemberValueVisitorImpl implements MemberValueVisitor {

	private Object value;
	
	private ConstPool constPool;
	
	public MemberValueVisitorImpl(Object value, ConstPool constPool) {
		this.value = value;
		this.constPool = constPool;
	}
	
	@Override
	public void visitAnnotationMemberValue(AnnotationMemberValue node) {
		node.setValue((Annotation) value);
	}

	@Override
	public void visitArrayMemberValue(ArrayMemberValue node) {
		List<MemberValue> memberValues = new ArrayList<MemberValue>();
		for (Object o: (Object[]) value) {
			try {
				MemberValue memberValue = FieldUtil.createMemberValue(o, constPool);
				memberValues.add(memberValue);
			} catch (NotFoundException e) {
			}
		}
		node.setValue( memberValues.toArray(new MemberValue[] {}));
	}

	@Override
	public void visitBooleanMemberValue(BooleanMemberValue node) {
		node.setValue( (boolean) value);
	}

	@Override
	public void visitByteMemberValue(ByteMemberValue node) {
		node.setValue( (byte) value);

	}

	@Override
	public void visitCharMemberValue(CharMemberValue node) {
		node.setValue( (char) value);

	}

	@Override
	public void visitDoubleMemberValue(DoubleMemberValue node) {
		node.setValue( (double) value);

	}

	@Override
	public void visitEnumMemberValue(EnumMemberValue node) {
		node.setValue( (String) value);

	}

	@Override
	public void visitFloatMemberValue(FloatMemberValue node) {
		node.setValue( (float) value);

	}

	@Override
	public void visitIntegerMemberValue(IntegerMemberValue node) {
		node.setValue( (int) value);

	}

	@Override
	public void visitLongMemberValue(LongMemberValue node) {
		node.setValue( (long) value);

	}

	@Override
	public void visitShortMemberValue(ShortMemberValue node) {
		node.setValue( (short) value);

	}

	@Override
	public void visitStringMemberValue(StringMemberValue node) {
		node.setValue( (String) value);

	}

	@Override
	public void visitClassMemberValue(ClassMemberValue node) {
		node.setValue( ((Class<?>) value).getName());
	}

}
